import "babel-polyfill";
import React from 'react';
import ReactDOM from 'react-dom';
import './index.less';
import './styles/css/bootstrap.css';
import { BrowserRouter, Route, Switch, Redirect } from 'react-router-dom';
import Home from './pages/front/home';
import Error from './pages/front/error';
import Protocol from './pages/front/protocol';
import WeiXin from './pages/front/weixin';
import GoodsDetail from './pages/goods/detail'
import registerServiceWorker from './registerServiceWorker';
import Cookies from 'js-cookie'
import Bundle from './utils/Bundle'

const LevelTest = (props) => (
	<Bundle load={() => import('./pages/tests/levelTest')}>
			{(LevelTest) => <LevelTest {...props}/>}
	</Bundle>
);
const PayOrder = (props) => (
	<Bundle load={() => import('./pages/goods/payOrder')}>
			{(PayOrder) => <PayOrder {...props}/>}
	</Bundle>
);
const UserHome = (props) => (
	<Bundle load={() => import('./pages/admin/home')}>
			{(UserHome) => <UserHome {...props}/>}
	</Bundle>
);
// import './styles/common'
// import Redirect from 'react-router-dom/Redirect';


const supportsHistory = 'pushState' in window.history

// 登陆了跳转到对应页,未登录跳转到登陆页
const FadingRoute = ({ component: Component, ...rest }) => {
  const isLogin = Cookies.get('token');
  let comp = ""
  if (isLogin) {
    comp = (<Route {...rest} render={props => {
      return (
      <Component {...props} />
    )}} />)
  } else {
    comp = (<Redirect to="/login/" />)
  }
  return comp
}
// 登陆了访问首页直接跳到个人中心,未登录跳转到对应页
const IndexRoute = ({ component: Component, ...rest }) => {
  const isLogin = Cookies.get('token');
  let comp = ""
  if (isLogin) {
    comp = (<Redirect to="/admin" />)
  } else {
    comp = (<Route {...rest} render={props => {
      return (
      <Component {...props} />
    )}} />)
  }
  return comp
}

ReactDOM.render(
	<BrowserRouter basename="/" forceRefresh={!supportsHistory}>
	  <Switch>
      <FadingRoute path="/admin" component={UserHome}/>
      <Route path="/error" component={Error}/>
      <Route path="/protocol" component={Protocol}/>
      <Route path="/weixin" component={WeiXin}/>
      <FadingRoute path="/goodsDetail" component={GoodsDetail}/>
      <FadingRoute path="/payOrder" component={PayOrder}/>
      <FadingRoute path="/levelTest" component={LevelTest}/>
      <IndexRoute path="/" component={Home}/>
	  </Switch>
	</BrowserRouter>, document.getElementById('root'));
registerServiceWorker();
